function TreeNode(val, left, right) {
  this.val = (val === undefined ? 0 : val)
  this.left = (left === undefined ? null : left)
  this.right = (right === undefined ? null : right)
}

function createTree(arr) {
  if (arr.length < 1)
    return null;
  let NodeArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == null) {
      NodeArr.push(null);
      continue;
    }
    let tempNode = new TreeNode(arr[i], null, null);
    NodeArr.push(tempNode);
  }
  for (let i = 0; i < arr.length; i++) {
    if (NodeArr[i] == null) continue;
    if (2 * i + 1 < arr.length)
      NodeArr[i].left = NodeArr[2 * i + 1];
    if (2 * i + 2 < arr.length)
      NodeArr[i].right = NodeArr[2 * i + 2];
  }
  return NodeArr[0];
}

function preShow(node) {
  if (node == null)
    return;
  console.log(node.val);
  preShow(node.left);
  preShow(node.right);
}
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
  // root = createTree(root);
  if(root == null)
    return true;
  // 层序遍历
  function levelOrder(root) {
    let queue = [{
      node: root,
      level: 0
    }], result = [];
    while(queue.length) {
      const obj = queue[0];
      if(result[obj.level]) {
        if(obj.node == null)
          result[obj.level].push('null');
        else
          result[obj.level].push(obj.node.val);
      } else {
        if(obj.node == null)
          result[obj.level] = ['null'];
        else
          result[obj.level] = [obj.node.val];
      }
      if(obj.node) {
        queue.push({
          node: obj.node.left,
          level: obj.level + 1
        });
      }
      if(obj.node) {
        queue.push({
          node: obj.node.right,
          level: obj.level + 1
        });
      }
      queue.splice(0, 1);
    }
    return result;
  }
  let arr = levelOrder(root);
  for(let i = 0; i < arr.length; i++) {
    for(let j = 0; j < arr[i].length / 2; j++) {
      if(arr[i][j] != arr[i][arr[i].length - 1 - j]) {
        return false;
      }
    }
  }
  return true;
};
let root = [1,2,2,3,4,4,3];
console.log(isSymmetric(root));
root = [1,2,2,null,3,null,3];
console.log(isSymmetric(root));
